package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//查询分院地址


@RestController
@RequestMapping(path = "/address")
@CrossOrigin
public class AddressController {


    @Reference(version = "1.0.0")
    private AddressService addressService;

    /**
     * 查询当月的预约数据
     * @return
     */
    @GetMapping( "/findAll")
    public Result findAll() {
        //分页查询预约信息
        try {
            List<Address> addresses = addressService.findAll();
            return new Result(true, MessageConstant.QUERY_ADDRESS_SUCCESS, addresses);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    @PostMapping(path = "/submit")//新增一个分院
    public Result submitAdreess(@RequestBody Address address){
        int count = 0;
        try {
            count = addressService.submit(address);
            if(count!= 0) {
                return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.ADD_ADDRESS_FAIL);
    }


    //根据id查询分院的信息
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable Integer id){
        try {
            Address address = this.addressService.findById(id);
            return new Result(true,MessageConstant.QUERY_ADDRESS_SUCCESS,address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

    //根据id删除信息
    @DeleteMapping(path = "/{id}")
    public Result delete(@PathVariable Integer id){
        int count = 0;
        try {
            count = this.addressService.delete(id);
            if(count==1) {
                return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.DELETE_ADDRESS_FAIL);
    }


    /**
     * 是否启用
     * @param id
     * @param enable
     * @return
     */
    @PutMapping("/{id}/{enable}")
    public Result updeteEnable(@PathVariable("id") Integer id, @PathVariable("enable") Integer enable) {
        this.addressService.updateEnable(id, enable);

        return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
    }

}
